SpringMVC @ResponseBody注解 json转换 返回406

1、描述

SpringMVC通过@RequestMapping注解,自动将对象转换为接送返回。浏览器请求后却返回406错误码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
@Controller
public class IndexAction {
@RequestMapping("/userList")
public @ResponseBody List<TXUser> userList(String updated_at){
System.out.println(updated_at);
TXUser user1=new TXUser();
user1.setAccount("a1");
user1.setCode("11");
user1.setCreated_at(new Date());
user1.setName("user1");
TXUser user2=new TXUser();
user2.setAccount("a2");
user2.setCode("22");
user2.setCreated_at(new Date());
user2.setName("user2");

List<TXUser> userslist=new ArrayList<TXUser>();
userslist.add(user1);
userslist.add(user2);
return userslist;
}

@RequestMapping("/index")
public String index(){
return "index";
}
}

2、原因

网上找过相关解决方法,但都是修改SpringMVC配置文件添加Converter。我对应修改后均无效。通过debug,发现返回406的原因是因为org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor中的writeWithMessageConverters方法出现异常。

1
2
3
4
5
6
7
8
9
10
List<MediaType> requestedMediaTypes = getAcceptableMediaTypes(servletRequest);
List<MediaType> producibleMediaTypes = getProducibleMediaTypes(servletRequest, returnValueClass);
Set<MediaType> compatibleMediaTypes = new LinkedHashSet<MediaType>();
for (MediaType requestedType : requestedMediaTypes) {
for (MediaType producibleType : producibleMediaTypes) {
if (requestedType.isCompatibleWith(producibleType)) {
compatibleMediaTypes.add(getMostSpecificMediaType(requestedType, producibleType));
}
}
}

以上代码中,各个变量的值最后分别为
变量|值
—|—
requestedMediaTypes|[text/html]
producibleMediaTypes|[application/json;charset=UTF-8,application/x-www-form-urlencoded;charset=UTF-8,application/json;charset=UTF-8,application/*+json;charset=UTF-8]
compatibleMediaTypes|[]

其中requestedMediaTypes为浏览器向SpringMVC的请求头,由于不匹配SpringMVC对应的请求头,最后返回406。

3、解决办法

找到对应原因,通过网络查找资料后,发现修改web.xml文件
原:

1
2
3
4
5
6
7
8
9
10
11
12
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>

修改后:

1
2
3
4
5
6
7
8
9
10
11
12
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

修改xml后,浏览器再次请求后,正常返回json,不会返回406错误码了。
此时再次debug,三个变量的值分别为
变量|值
–|—
requestedMediaTypes|[text/html,application/xhtml+xml, image/webp,application/xml;q=0.9,* /;q=0.8]
producibleMediaTypes|[application/json;charset=UTF-8,application/x-www-form-urlencoded;charset=UTF-8,application/json;charset=UTF-8, application/
+json;charset=UTF-8]
compatibleMediaTypes|[application/json;charset=UTF-8;q=0.8,application/x-www-form-urlencoded;charset=UTF-8;q=0.8,application/*+json;charset=UTF-8;q=0.8]

>